package com.xuan.utils;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.security.Keys;

import javax.crypto.SecretKey;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class JwtTokenUtil {

    // 安全密钥（推荐长度至少256位）
    private static final String SECRET = "your-256-bit-secret-please-change-this-to-random-string";

    // 生成安全的密钥
    private static SecretKey getSecretKey() {
        return Keys.hmacShaKeyFor(SECRET.getBytes());
    }

    /**
     * 生成Token
     * @param userId 用户id
     * @return JWT令牌
     */
    public static String generateToken(Long userId) {
        Map<String, Object> claims = new HashMap<>();
        claims.put("userId", userId);

        return Jwts.builder()
                .setClaims(claims)
                .setSubject(String.valueOf(userId))
                .setIssuedAt(new Date())
                .signWith(getSecretKey(), SignatureAlgorithm.HS256)
                .compact();
    }
}